package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    List<Map<String, Object>> list = new ArrayList<>();
    String titles[] = {"桌子", "苹果", "蛋糕", "线衣", "围巾", "猕猴桃"};
    String price[] = {"价格： 1800元", "价格： 10元/kg", "价格： 300元", "价格： 350元", "价格： 180元", "价格： 18元/kg"};
    int icons[] = {R.drawable.table, R.drawable.apple, R.drawable.cake, R.drawable.wireclothes, R.drawable.scarf, R.drawable.kiwifruit};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text1_listview);
        for (int i = 0; i < titles.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("title", titles[i]);
            map.put("price", price[i]);
            map.put("icon", icons[i]);
            list.add(map);
        }
        SimpleAdapter sm = new SimpleAdapter(this, list, R.layout.text_item, new String[]{"title", "price", "icon"}, new int[]{R.id.tv1,R.id.tv2, R.id.image});
        ListView view = findViewById(R.id.lv);
        view.setAdapter(sm);
        view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "您点击了" + titles[position], Toast.LENGTH_SHORT).show();
            }
        });
    }
}